package object recursion {

  class ListNode(var _x: Int = 0) {
    var next: ListNode = _
    var x: Int = _x

    override def toString: String = s"Node[$x] -> ${if (next == null) "[]" else next}"
  }

  def nodes(it: Iterable[Int]): ListNode = {
    val nodes = it.map(new ListNode(_)).toList
    nodes.sliding(2).foreach { a => a(0).next = a(1) }
    nodes.last.next = null
    nodes.head
  }

  def nodes(n: Int): ListNode = nodes(1 to n)

  def nodes(a: Int*): ListNode = nodes(a)

  class TreeNode(var _value: Int) {
    var value: Int = _value
    var left: TreeNode = null
    var right: TreeNode = null

    override def toString: String = {
      if (left == null && right == null) s"$value"
      else if (left == null) s"$value -> (_, $right)"
      else if (right == null) s"$value -> ($left, _)"
      else s"$value -> ($left, $right)"
    }
  }

}
